package com.msb.mallsearch.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.msb.common.dao.es.SkuESModel;
import com.msb.mallsearch.config.MallElasticSearchConfiguration;
import com.msb.mallsearch.constant.ESconstant;
import com.msb.mallsearch.service.ElasticSearchSaveService;
import lombok.SneakyThrows;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

/**
 * @Auther: xixifeng
 * @Date: 2022/10/18 - 10 - 18 - 20:14
 * @Description: com.msb.mallsearch.service.impl
 * @version: 1.0
 */

@Service("SpusearchService")
public class ElasticSearchSaveServiceImpl implements ElasticSearchSaveService {

    @Autowired
    RestHighLevelClient restHighLevelClient;

    /**
     * 实现上架数据存储到ES的操作
     * @param skuESModels
     * @return
     */

  /* @Override
    public Boolean productStatusUp(List<SkuESModel> skuESModels)  {

        // 创建对应索引库 --》 创建对应的Product的映射
        // BulkRequest bulkRequest, RequestOptions options
        BulkRequest bulkRequest = new BulkRequest();

        for (SkuESModel skuESModel : skuESModels) {

            // 绑定对应的索引库  product
            IndexRequest indexRequest = new IndexRequest(ESconstant.PRODUCT_INDEX);
            // 设置id
            indexRequest.id(skuESModel.getSkuId().toString());
            // 设置文档
            ObjectMapper objectMapper = new ObjectMapper();
            String json = null;
            try {
                json = objectMapper.writeValueAsString(skuESModel);
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
            indexRequest.source(json, XContentType.JSON);
            // 转换后的数据封装到Bulk中
            bulkRequest.add(indexRequest);
        }
            // 批量向ElasticSearch中保存数据
        BulkResponse bulk = null;
        try {
            bulk = restHighLevelClient.bulk(bulkRequest, MallElasticSearchConfiguration.COMMON_OPTIONS);
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 获取批量操作是否有错误,下面这个方法是返回值默认是false
        boolean b = bulk.hasFailures();

        return !b;

    }*/

    /**
     * 实现上架数据存储到ES的操作
     * @param skuESModels
     * @return
     */
    @Override
    public Boolean productStatusUp(List<SkuESModel> skuESModels) throws IOException {
        // 创建对应索引库 --》 创建对应的Product的映射
        //todo BulkRequest bulkRequest, RequestOptions options
        BulkRequest bulkRequest = new BulkRequest();
        for (SkuESModel skuESModel : skuESModels) {
            // 绑定对应的索引库  product
            IndexRequest indexRequest = new IndexRequest(ESconstant.PRODUCT_INDEX);
            // 设置id
            indexRequest.id(skuESModel.getSkuId().toString());
            // 设置文档
            ObjectMapper mapper = new ObjectMapper();
            String json = mapper.writeValueAsString(skuESModel);
            indexRequest.source(json, XContentType.JSON);
            // 转换后的数据封装到Bulk中
            bulkRequest.add(indexRequest);
        }
        // 批量向ElasticSearch中保存数据
        BulkResponse bulk = restHighLevelClient.bulk(bulkRequest, MallElasticSearchConfiguration.COMMON_OPTIONS);
        // 获取批量操作是否有错误
        boolean b = bulk.hasFailures();
        /*for (BulkItemResponse item : bulk.getItems()) {
            item.get
        }*/
        return !b;
    }



}
